package com.js.domian.model;

import com.github.pagehelper.Page;
import lombok.Data;

import java.util.HashMap;

@Data
public class AjaxResult extends HashMap<String, Object> {
    /** 状态 */
    public static final String SUCCESS_TAG = "success";

    /** 返回内容 */
    public static final String MSG_TAG = "msg";

    /** 数据对象 */
    public static final String DATA_TAG = "data";

    /** 总数 */
    public static final String TOTAL_TAG = "total";

    public AjaxResult(boolean success){
        super.put(SUCCESS_TAG, success);
    }

    public AjaxResult(Object data, boolean success){
        super.put(SUCCESS_TAG, success);
        super.put(DATA_TAG, data);
    }

    public AjaxResult(String msg, boolean success){
        super.put(SUCCESS_TAG, success);
        super.put(MSG_TAG, msg);
    }

    public static AjaxResult ok(){
        return new AjaxResult(true);
    }

    public static AjaxResult ok(Object data){
        AjaxResult ajaxResult = new AjaxResult(data, true);
        if(data instanceof Page){
            ajaxResult.put(TOTAL_TAG, ((Page)data).getTotal());
        }
        return ajaxResult;
    }

    public static AjaxResult fail(){
        return new AjaxResult(false);
    }

    public static AjaxResult fail(String msg){
        return new AjaxResult(msg, false);
    }

    /**
     * 方便链式调用
     *
     * @param key 键
     * @param value 值
     * @return 数据对象
     */
    @Override
    public AjaxResult put(String key, Object value)
    {
        super.put(key, value);
        return this;
    }
}
